package models

import (
	"errors"

	orm "go-papers/database"
)

type DictType struct {
	DictCode int `gorm:"-" json:"dictCode"` //字典编码

	DictName string `gorm:"type:varchar(128);" json:"dictName"` //字典名称
	DictType string `gorm:"type:varchar(128);" json:"dictType"` //字典类型
}

type DictTypePage struct {
	DictName string `gorm:"type:varchar(128);" json:"dictName"` //字典名称
	DictType string `gorm:"type:varchar(128);" json:"dictType"` //字典类型

	DictCode   int    `gorm:"primary_key;AUTO_INCREMENT" json:"dictCode" example:"1"` //字典编码
	DictSort   string `gorm:"type:int(4);" json:"dictSort"`                           //显示顺序
	DictLabel  string `gorm:"type:varchar(128);" json:"dictLabel"`                    //数据标签
	DictValue  string `gorm:"type:varchar(255);" json:"dictValue"`                    //数据键值
	DictStatus string `gorm:"-" json:"dictStatus"`                                    //状态0 正常 ， 1 关闭

	DataScope string `gorm:"-" json:"dataScope"`                //
	Params    string `gorm:"-" json:"params"`                   //
	CreateBy  string `gorm:"type:varchar(11);" json:"createBy"` //创建者
	UpdateBy  string `gorm:"type:varchar(11);" json:"updateBy"` //更新者
	Remark    string `gorm:"type:varchar(255);" json:"remark"`  //备注
	BaseModel
}

func (DictType) TableName() string {
	return "sys_dict_type"
}

func (e *DictType) Create() (DictType, error) {
	var doc DictType

	i := 0
	orm.Eloquent.Table(e.TableName()).Where("dict_name=? or dict_type = ?", e.DictName, e.DictType).Count(&i)
	if i > 0 {
		return doc, errors.New("字典名称或者字典类型已经存在！")
	}

	result := orm.Eloquent.Table(e.TableName()).Create(&e)
	if result.Error != nil {
		err := result.Error
		return doc, err
	}
	doc = *e
	return doc, nil
}
func (e *DictType) GetData() (DictTypePage, error) {
	var doc DictTypePage

	table := orm.Eloquent.Table(e.TableName())
	table = table.Joins("inner join sys_dict_data on sys_dict_data.dict_type = sys_dict_type.dict_type")
	if e.DictName != "" {
		table = table.Where("sys_dict_type.dict_name like ?", "%"+e.DictName+"%")
	}
	if e.DictType != "" {
		table = table.Where("sys_dict_type.dict_type = ?", e.DictType)
	}
	if e.DictCode != 0 {
		table = table.Where("sys_dict_data.dict_code = ?", e.DictCode)
	}

	if err := table.First(&doc).Error; err != nil {
		return doc, err
	}
	return doc, nil
}

func (e *DictType) Get() (DictType, error) {
	var doc DictType

	table := orm.Eloquent.Table(e.TableName())
	if e.DictName != "" {
		table = table.Where("dict_name like ?", "%"+e.DictName+"%")
	}
	if e.DictType != "" {
		table = table.Where("dict_type = ?", e.DictType)
	}

	if err := table.First(&doc).Error; err != nil {
		return doc, err
	}
	return doc, nil
}

func (e *DictType) GetList() ([]DictType, error) {
	var doc []DictType

	table := orm.Eloquent.Table(e.TableName())
	if e.DictName != "" {
		table = table.Where("dict_name like ?", "%"+e.DictName+"%")
	}
	if e.DictType != "" {
		table = table.Where("dict_type = ?", e.DictType)
	}

	if err := table.Find(&doc).Error; err != nil {
		return doc, err
	}
	return doc, nil
}

func (e *DictType) GetPage(pageSize int, pageIndex int) ([]DictTypePage, int, error) {
	var doc []DictTypePage

	table := orm.Eloquent.Select("sys_dict_type.*,sys_dict_data.*").Table(e.TableName())
	table = table.Joins("inner join sys_dict_data on sys_dict_data.dict_type = sys_dict_type.dict_type")
	if e.DictName != "" {
		table = table.Where("sys_dict_type.dict_name like ?", "%"+e.DictName+"%")
	}
	if e.DictType != "" {
		table = table.Where("sys_dict_type.dict_type like ?", "%"+e.DictType+"%")
	}
	table.Where("`sys_dict_data.deleted_at` IS NULL")
	table.Order("sys_dict_data.dict_code asc,sys_dict_data.dict_sort asc ")
	var count int
	if err := table.Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil {
		return nil, 0, err
	}
	table.Count(&count)
	return doc, count, nil
}

func (e *DictType) Update(nameType string) (update DictType, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("dict_type = ?", nameType).First(&update).Error; err != nil {
		return
	}
	//参数1:是要修改的数据
	//参数2:是修改的数据
	if err = orm.Eloquent.Table(e.TableName()).Model(&update).Updates(&e).Error; err != nil {
		return
	}
	return
}
